package com.csi.dao.impl;

import com.csi.dao.PermissionDao;
import com.csi.domain.Permission;
import com.csi.utils.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PermissionDaoImpl implements PermissionDao {
    @Override
    public List<Permission> findAll() throws SQLException {
        Connection connection = DruidUtils.getConnection();

        String sql = """
        SELECT * FROM permission
        """;
        PreparedStatement ps = connection.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();

        List<Permission> permissions = new ArrayList<>();
        while (rs.next()){
            Permission permission = new Permission();
            permission.setPermissionId(rs.getInt("permission_id"));
            permission.setPermissionName(rs.getString("permission_name"));
            permission.setPermissionDesc(rs.getString("permission_desc"));
            permissions.add(permission);
        }

        DruidUtils.close(connection);
        return permissions;
    }
}
